package cn.stylefeng.guns.modular.system.controller;

import cn.stylefeng.guns.base.log.BussinessLog;
import cn.stylefeng.guns.base.pojo.node.ZTreeNode;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.base.shiro.annotion.Permission;
import cn.stylefeng.guns.modular.system.entity.UserAreaRelation;
import cn.stylefeng.guns.modular.system.model.params.UserAreaRelationParam;
import cn.stylefeng.guns.modular.system.service.AddressService;
import cn.stylefeng.guns.modular.system.service.UserAreaRelationService;
import cn.stylefeng.guns.sys.core.constant.Const;
import cn.stylefeng.guns.sys.core.constant.dictmap.RoleDict;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.stream.Collectors;


/**
 * 角色和菜单关联表控制器
 *
 * @author 小飞
 * @Date 2019-08-03 16:27:53
 */
@Controller
@RequestMapping("/userAreaRelation")
public class UserAreaRelationController extends BaseController {

    private String PREFIX = "/userAreaRelation";

    @Autowired
    private AddressService addressService;

    @Autowired
    private UserAreaRelationService userAreaRelationService;

    /**
     * 跳转到主页面
     *
     * @author 小飞
     * @Date 2019-08-03
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/userAreaRelation.html";
    }

    /**
     * 新增页面
     *
     * @author 小飞
     * @Date 2019-08-03
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/userAreaRelation_add.html";
    }

    /**
     * 编辑页面
     *
     * @author 小飞
     * @Date 2019-08-03
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/userAreaRelation_edit.html";
    }

    /**
     * 跳转到权限分配
     *
     * @author fengshuonan
     * @Date 2018/12/23 6:31 PM
     */
    @RequestMapping(value = "/userArea/{userId}")
    public String userArea(@PathVariable("userId") Long userId, Model model) {
        if (ToolUtil.isEmpty(userId)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        model.addAttribute("userId", userId);
        return PREFIX + "/user_area.html";
    }


    /**
     * 新增接口
     *
     * @author 小飞
     * @Date 2019-08-03
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(UserAreaRelationParam userAreaRelationParam) {
        this.userAreaRelationService.add(userAreaRelationParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author 小飞
     * @Date 2019-08-03
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(UserAreaRelationParam userAreaRelationParam) {
        this.userAreaRelationService.update(userAreaRelationParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author 小飞
     * @Date 2019-08-03
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(UserAreaRelationParam userAreaRelationParam) {
        this.userAreaRelationService.delete(userAreaRelationParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author 小飞
     * @Date 2019-08-03
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(UserAreaRelationParam userAreaRelationParam) {
        UserAreaRelation detail = this.userAreaRelationService.getById(userAreaRelationParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author 小飞
     * @Date 2019-08-03
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(UserAreaRelationParam userAreaRelationParam) {
        return this.userAreaRelationService.findPageBySpec(userAreaRelationParam);
    }


    /**
     * 获取用户的地址列表
     *
     * @author fengshuonan
     * @Date 2018/12/23 5:54 PM
     */
    @RequestMapping(value = "/areaByUser/{userId}")
    @ResponseBody
    public List<ZTreeNode> areaByUser(@PathVariable Long userId) {
        List<UserAreaRelation> userAreaRelations = userAreaRelationService.getBaseMapper().selectList(new QueryWrapper<UserAreaRelation>()
        .lambda().eq(UserAreaRelation::getUserId,userId));
        List<Long> areaId = userAreaRelations.stream().map(s -> s.getUserId()).collect(Collectors.toList());
        if (ToolUtil.isEmpty(areaId)) {
            return addressService.tree();
        } else {
            return this.userAreaRelationService.menuTreeListByMenuIds(areaId);
        }
    }
    /**
     * 配置区域
     *
     * @author fengshuonan
     * @Date 2018/12/23 6:31 PM
     */
    @RequestMapping("/setAuthority")
    @BussinessLog(value = "配置区域", key = "userId,ids", dict = RoleDict.class)
    @Permission(Const.ADMIN_NAME)
    @ResponseBody
    public ResponseData setAuthority(@RequestParam("userId") Long userId, @RequestParam("ids") String ids) {
        if (ToolUtil.isOneEmpty(userId)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        this.userAreaRelationService.setAuthority(userId, ids);
        return SUCCESS_TIP;
    }


}


